#include <cstdio>
#include "UI.h"
#include "element/Input.h"
#include "element/Map.h"
#include <tool.h>
#include <tinycthread.h>
//处理所有缓存的message
extern Map m;
bool ListeningKey[256];
long long KeyPressing[256];
bool LeftKeyPressing,RightKeyPressing;
//默认监听鼠标左右键的按下，要监听键盘键位需要注册SetListenKey

bool IsLeftKeyPressing() {
    return LeftKeyPressing;
}
bool IsRightKeyPressing() {
    return RightKeyPressing;
}
void SetListenKey(char key) {
    ListeningKey[key] = true;
}
long long GetKeyPressTime(char key) {
    return KeyPressing[key];
}
void handleLocalKey()
{
    ExMessage msg;
    while(true) {
        msg = getmessage(); //阻塞获取按键
        switch (msg.message)
        {
        case WM_CHAR:
            //printf("检测到字符%c(%d)\n",msg.ch,msg.ch);
            handleChar(msg.ch);
            break;
        case WM_KEYDOWN:
            if (ListeningKey[msg.vkcode])
                KeyPressing[msg.vkcode] = TimeMilliSecond();
            //printf("有按键被按下%c(%d)%c(%d),时间%lld\n",msg.vkcode,msg.vkcode,msg.scancode,msg.scancode,KeyPressing[msg.vkcode]);
            break;
        case WM_KEYUP:
            if (ListeningKey[msg.vkcode])
                KeyPressing[msg.vkcode] = 0;
            //printf("有按键被弹起%c(%d)%c(%d)\n",msg.vkcode,msg.vkcode,msg.scancode,msg.scancode);
            break;
        case WM_LBUTTONUP:
            LeftKeyPressing = false;
            //printf("鼠标左键被弹起，位置(%d,%d)\n",msg.x,msg.y);
            handleLeftUp(msg.x,msg.y);
            break;
        case WM_LBUTTONDOWN:
            LeftKeyPressing = true;
            //printf("鼠标左键被按下，位置(%d,%d)\n",msg.x,msg.y);
            break;
        case WM_RBUTTONDOWN:
            RightKeyPressing = true;
//            printf("相对坐标为%d %d\n",msg.x-m.getArea().left,msg.y-m.getArea().top);
//            printf("地图坐标为%d %d\n",m.getBlockPositionByPixel(msg.x,msg.y).i,m.getBlockPositionByPixel(msg.x,msg.y).j);
//            printf("这个块的等级为%d\n",m.getBlockLevelByPixel(msg.x,msg.y));
//            m.setBlockLevelByPixel(msg.x,msg.y,m.getBlockLevelByPixel(msg.x,msg.y)+1);
            break;
        case WM_RBUTTONUP:
            RightKeyPressing = false;
            break;
        case WM_LBUTTONDBLCLK:
            handleDoubleClick(msg.x,msg.y);
            printf("检测到双击，位置(%d,%d)\n",msg.x,msg.y);
            break;
        case WM_MOUSEMOVE:
            handleMouseMove(msg.x,msg.y);
            break;
        }
    }
}

void handleLeftUp(int x,int y)
{
    for (size_t i = 0; i < nowInteractiveElementCount; i++)
    {
        if(InteractiveElements[i] == nullptr) {
            continue;
        }
        if(InteractiveElements[i]->getUI() == GetNowUI() && in_rect(x, y, InteractiveElements[i]->getArea())) {
            InteractiveElements[i]->getCallback()((void*)InteractiveElements[i]);
        }
    }
    Input* in;
    for (int i = 0; i < nowInputElementCount; ++i) {
        if(InputElements[i] == nullptr) {
            continue;
        }
        in = (Input*)InputElements[i];
        if(in->getUI() != GetNowUI()) {
            continue;
        }
        if (!in->isFocus() && in_rect(x,y,InputElements[i]->getArea())) {
            in->onFocus();
        } else if(in->isFocus() && !in_rect(x,y,InputElements[i]->getArea())) {
            in->onBlur();
        }
    }
}
void handleChar(char c)
{
    Input* in;
    for (int i = 0; i < nowInputElementCount; ++i) {
        if(InputElements[i] == nullptr) {
            continue;
        }
        in = (Input*)InputElements[i];
        if(in->isFocus() && in->getUI() == GetNowUI()) {
            in->onType(c);
        }
    }
}
void handleDoubleClick(int x,int y)
{
    for (size_t i = 0; i < nowDoubleClickCount; i++)
    {
        if(DoubleClickElements[i] == nullptr) {
            continue;
        }
        //printf("%d %d %d %d %d %d \n",GetNowUI(),DoubleClickElements[i]->getUI(),DoubleClickElements[i]->getArea().top,DoubleClickElements[i]->getArea().left,DoubleClickElements[i]->getArea().bottom,DoubleClickElements[i]->getArea().right);
        if(DoubleClickElements[i]->getUI() == GetNowUI() && in_rect(x, y, DoubleClickElements[i]->getArea())) {
            DoubleClickElements[i]->getDoubleClickCallback()((void*)DoubleClickElements[i]);
        }
    }
}
void handleMouseMove(int x,int y)
{

}

